﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookingSystemBLL;
using System.Data;
using BookingSystemModel;

public partial class LookupUser : System.Web.UI.Page
{
    private user UserInfo = new user();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
   
    //编辑乘客信息
    protected void GridView_lookup_RowEditing(object sender, GridViewEditEventArgs e)
    {
         GridView_lookup.EditIndex = e.NewEditIndex;
         Bind();
    }
    //删除乘客
    protected void GridView_lookup_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = (this.GridView_lookup.Rows[e.RowIndex].FindControl("Label3") as Label).Text;
        if (FlightBLL.DeleteFlight(id) > 0)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除成功！！');</script>");
            Bind();
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除失败！！');</script>");
        }
    }
    //更新乘客信息
    protected void GridView_lookup_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextToUser(sender, e);
        if (UserBLL.UpdateUserInfo(UserInfo)>0)
        {
            GridView_lookup.EditIndex = -1;
            Bind();
        } 
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改失败！');</script>");
        }
    }
    public void TextToUser(object sender, GridViewUpdateEventArgs e)
    {
        UserInfo.UserID = Convert.ToInt32((this.GridView_lookup.Rows[e.RowIndex].FindControl("Label3") as Label).Text);
        UserInfo.UserName = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[1].FindControl("TextBox1")).Text);
        UserInfo.UserIDNumber = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[2].FindControl("TextBox2")).Text);
        UserInfo.UserNickName = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[3].FindControl("TextBox4")).Text);
        UserInfo.Pwd = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[4].FindControl("TextBox5")).Text);
        UserInfo.Sex = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[5].FindControl("TextBox6")).Text);
        UserInfo.Phone = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[6].FindControl("TextBox7")).Text);
        UserInfo.Email = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[7].FindControl("TextBox8")).Text);
        UserInfo.Address = Convert.ToString(((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[8].FindControl("TextBox9")).Text);
        UserInfo.RegisterTime = DateTime.Parse(string.Format("{0:yyyy}/{0:MM}/{0:dd}", ((TextBox)GridView_lookup.Rows[e.RowIndex].Cells[9].FindControl("TextBox10")).Text));
    }
    //取消编辑
    protected void GridView_lookup_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView_lookup.EditIndex = -1;
        Bind();
    }

    public void Bind()
    {
        DataSet ds = UserBLL.SearchUser();
        this.GridView_lookup.DataSource = ds.Tables[0];
        this.GridView_lookup.DataBind();

    }
}